package market

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/market"
	"git.myscrm.cn/golang/stark/v4"
)

type outboundCrowdRepository struct {
}

// NewCustomerPoolRepository 客户池
func NewOutboundCrowdRepository() OutboundCrowdRepositoryIface {
	return &outboundCrowdRepository{}
}

func (r *outboundCrowdRepository) GetCrowdList(ctx context.Context, request *pb.GetCrowdListRequest) (*pb.GetCrowdListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdList-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.GetCrowdList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdList-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) GetCrowdDetail(ctx context.Context, request *pb.GetCrowdDetailRequest) (*pb.GetCrowdDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdDetail-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.GetCrowdDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdDetail-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) AddCrowd(ctx context.Context, request *pb.AddCrowdRequest) (*pb.AddCrowdResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "AddCrowd-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.AddCrowd(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "AddCrowd-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) DeleteCrowd(ctx context.Context, request *pb.DeleteCrowdRequest) (*pb.DeleteCrowdResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "DeleteCrowd-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.DeleteCrowd(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "DeleteCrowd-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) GetCrowdByWinbid(ctx context.Context, request *pb.GetCrowdByWinbidRequest) (*pb.GetCrowdByWinbidResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByWinbid-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.GetCrowdByWinbid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByWinbid-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) GetCrowdByDeveloper(ctx context.Context, request *pb.GetCrowdByDeveloperRequest) (*pb.GetCrowdByDeveloperResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByWinbid-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.GetCrowdByDeveloper(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByWinbid-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) GetCrowdByRpa(ctx context.Context, request *pb.GetCrowdByRpaRequest) (*pb.GetCrowdByRpaResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByRpa-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.GetCrowdByRpa(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByRpa-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) GetRpaContactMobile(ctx context.Context, request *pb.GetRpaContactMobileRequest) (*pb.GetRpaContactMobileResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetRpaContactMobile-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.GetRpaContactMobile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetRpaContactMobile-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) CrowdDetailWinbid(ctx context.Context, request *pb.CrowdDetailWinbidRequest) (*pb.CrowdDetailWinbidResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "CrowdDetailWinbid-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.CrowdDetailWinbid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CrowdDetailWinbid-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *outboundCrowdRepository) CrowdDetailDeveloper(ctx context.Context, request *pb.CrowdDetailDeveloperRequest) (*pb.CrowdDetailDeveloperResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "CrowdDetailDeveloper-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundCrowdServiceClient(conn)
	res, err := client.CrowdDetailDeveloper(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CrowdDetailDeveloper-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}
